package com.wuming.house.api;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.HouseType;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.HouseTag;
import com.wuming.house.model.Secondhouse;
import com.wuming.house.service.HouseTagService;
import com.wuming.house.service.SecondhouseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 二手房的接口
 */
@Controller
@RequestMapping("/api/secondhouse")
public class SecondhouseApi {

    private static Logger logger = LoggerFactory.getLogger(SecondhouseApi.class);

    @Autowired
    private SecondhouseService secondhouseService;

    @Autowired
    private HouseTagService houseTagService;

    /**
     * 根据分类查询房产信息
     *
     * @return 分组房产信息
     */
    @ResponseBody
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public JsonResult getAllSecondHouse(@RequestParam(required = false) String houseDesc, @RequestParam(required = false) String area, @RequestParam(required = false) String price, @RequestParam(required = false) String layout) {
        try {
            //查询分类房产信息
            List<Secondhouse> houses = secondhouseService.getAllSecondhouse(houseDesc, area, price, layout);
            for (Secondhouse sh : houses) {
                List<HouseTag> houseTags = houseTagService.getHouseTagByHouseId(sh.getId(), HouseType.SECOND_HOUSE.getValue().intValue());
                sh.setHouseTags(houseTags);
            }
            return new JsonResult(houses, "查询分类房产信息成功.", true);
        } catch (BusinessException be) {
            logger.error("查询分类房产信息失败,异常:{}", be.getMessage());
            return new JsonResult(null, "查询分类房产信息失败.", false);
        } catch (Exception e) {
            logger.error("查询分类房产信息失败,异常:{}", e.getMessage());
            return new JsonResult(null, "查询分类房产信息失败.", false);
        }
    }

    /**
     * 根据楼盘id查询楼盘图片
     *
     * @param houseId 楼盘id
     * @return 楼盘图片
     */
//    @ResponseBody
//    @RequestMapping(value = "/house/img/{houseId}", method = RequestMethod.GET)
//    public JsonResult getHouseImgById(@PathVariable Long houseId) {
//        try {
//            List<HouseImg> houseImgs = houseImgService.getHouseImgByHouseId(houseId);
//            return new JsonResult(houseImgs, "查询楼盘图片信息成功.", true);
//        } catch (BusinessException be) {
//            logger.error("查询楼盘图片信息失败,异常:{}", be.getMessage());
//            return new JsonResult(null, "查询楼盘图片信息失败.", false);
//        } catch (Exception e) {
//            logger.error("查询楼盘图片信息失败,异常:{}", e.getMessage());
//            return new JsonResult(null, "查询楼盘图片信息失败.", false);
//        }
//    }
}
